import { message } from "antd";
import Cookie from "./cookie";
import { open, close } from "./obj/slices/loadingSlice";
const request = (url, params = {}, method = "GET", headers = {}) => {
  var fetchResult;
  try {
    if (method.toLowerCase() === "post") {
      fetchResult = fetch("http://localhost:50000/api" + url, {
        method: method,
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + Cookie.get("token"),
          ...headers,
        },
        body: JSON.stringify(params),
      }).then((response) => {
        return response.json();
      });
    } else if (method.toLowerCase() === "get") {
      fetchResult = fetch(
        "http://localhost:50000/api" +
          url +
          "?" +
          new URLSearchParams(params).toString(),
        {
          method: method,
          headers: {
            "Content-Type": "application/json",
            Authorization: "Bearer " + Cookie.get("token"),
            ...headers,
          },
        }
      ).then((response) => {
        return response.json();
      });
    } else {
      console.error("只支持GET,POST方法");
      return Promise.reject("只支持GET,POST方法");
    }
    return fetchResult;
  } catch (e) {
    message.error("服务器未响应");
    return Promise.reject("服务器未响应");
  }
};

const requestDispatch = (
  dispatch,
  url,
  params = {},
  method = "GET",
  headers = {}
) => {
  dispatch(open());
  var fetchResult;
  try {
    if (method.toLowerCase() === "post") {
      fetchResult = fetch("http://localhost:50000/api" + url, {
        method: method,
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + Cookie.get("token"),
          ...headers,
        },
        body: JSON.stringify(params),
      }).then((response) => {
        dispatch(close());
        return response.json();
      });
    } else if (method.toLowerCase() === "get") {
      fetchResult = fetch(
        "http://localhost:50000/api" +
          url +
          "?" +
          new URLSearchParams(params).toString(),
        {
          method: method,
          headers: {
            "Content-Type": "application/json",
            Authorization: "Bearer " + Cookie.get("token"),
            ...headers,
          },
        }
      ).then((response) => {
        dispatch(close());
        return response.json();
      });
    } else {
      dispatch(close());
      console.error("只支持GET,POST方法");
      return Promise.reject("只支持GET,POST方法");
    }
    return fetchResult;
  } catch (e) {
    dispatch(close());
    message.error("服务器未响应");
    return Promise.reject("服务器未响应");
  }
};

const getParams = (paramName) => {
  var urlSplits = window.location.href.split("?");
  if (urlSplits.length > 1) {
    return new URLSearchParams(urlSplits[1]).get(paramName);
  }
  return null;
};

// 只要数组长度和内容都一样，就认为是一样的
const isSameArray = (arr1, arr2) => {
  if (arr1.length !== arr2.length) {
    return false;
  }
  arr1.sort();
  arr2.sort();
  for (let i = 0; i < arr1.length; i++) {
    if (arr1[i] !== arr2[i]) {
      return false;
    }
  }
  return true;
};
const range = (start, end) => {
  return Array.from({ length: end - start + 1 }, (_, i) => start + i);
};
export { request, requestDispatch, getParams, isSameArray, range };
